﻿using Engine.Result;

using Scheduler;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

using EventArgs = Scheduler.EventArgs;

namespace Engine.Middlewares
{
    /// <summary>
    /// 这是一个假表
    /// </summary>
    internal class VirtualSourceMeterUnitMiddleware : Middleware
    {
        public VirtualSourceMeterUnitMiddleware(string str)
        {
            Console.WriteLine($"设备ID:{str}");
        }
        public override bool Filter(Context ctx, EventArgs args)
        {
            //if (args.Id != EventId.Handle) return false;
            return true;
        }

        protected override void DoHandleEvent(Context ctx, EventArgs args)
        {
            switch (args.Id)
            {
                case EventId.Init:
                    //TODO 链接测试机
                    break;
                case EventId.Cleanup:
                    break;
                case EventId.PassBegin:
                    break;
                case EventId.PassEnd:
                    break;
                case EventId.Handle:
                    HandleItem(ctx  as WaferTesterContext,  args);
                    break;
                case EventId.UserDefinedEventId:
                    break;
                default:
                    break;
            }
        }

        private void HandleItem(WaferTesterContext ctx, Scheduler.EventArgs userData)
        {

            
            if (userData.UserData[2] is ChipResult cr)
            {
                var DATA = ctx.Result.GetChipResultById((cr.Id));

                if (userData.UserData != null)
                    DATA._itemResults.Add(new List<ItemResult>()
                    {
                        new ItemResult()
                        {
                            Data = { (int)userData.UserData[0], 1, 1, 1 }, Flags = ItemResultFlags.Ok
                        }
                    });

                //DATA._itemResults[(int)userData.UserData[1]].AddRange(new ItemResult[5]);
            }

            Thread.Sleep(3000);
        }
    }
}
